﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DiscreteMath.Tests
{
    static class PredicateCombiner
    {
        public static Func<string, bool> CombineUnion(Func<string, bool> first, Func<string, bool> second)
        {
            return (word) => first(word) || second(word);
        }

        public static Func<string, bool> CombineIntersection(Func<string, bool> first, Func<string, bool> second)
        {
            return (word) => first(word) && second(word);
        }

        public static Func<string, bool> CombineConcatenation(Func<string, bool> first, Func<string, bool> second)
        {
            Func<string, bool> test = (word) =>
            {
                if (word.Length == 0)
                    return first(word) && second(word);

                for (int k = 0; k < word.Length; k++)
                {
                    string firstPart = word.Substring(0, k);
                    string secondPart = word.Substring(k);

                    if (first(firstPart) && second(secondPart))
                        return true;
                }
                return false;
            };

            return test;
        }
    }
}
